import request from '@/utils/request'
import { getDictionaryDataSelector } from '@/api/systemData/dictionary'
import { previewDataInterface } from '@/api/systemData/dataInterface'
import { getSelector } from '@/api/crm/school'
import { getCustomerInfo } from '@/api/crm/customer'
export default {
  components: {},
  props: [],
  data() {
    return {
      loading: false,
      visible: false,
      isDetail: false,
      dataForm: {
        id: '',
        area: undefined,
        customerId: undefined,
        brand: undefined,
        managerId: undefined,
        school: undefined,
        payMethod: undefined,
        franchiseFee: undefined,
        isCompletePay: undefined,
        finalPayDate: false,
        contractPolicy: undefined,
        orderStatus: undefined,
        amount: undefined,
        paymentDate: undefined,
        paymentMethod: undefined,
        customerName: undefined,
        screenshot: []
      },
      rules: {
        customerId: [
          {
            required: true,
            message: '请选择客户',
            trigger: 'change'
          }
        ],
        brand: [
          {
            required: true,
            message: '请输入收款品牌',
            trigger: 'change'
          }
        ],
        managerId: [
          {
            required: true,
            message: '请选择招商经理',
            trigger: 'change'
          }
        ],
        school: [
          {
            required: true,
            message: '请输入意向小学/中学',
            trigger: 'blur'
          }
        ],
        payMethod: [
          {
            required: true,
            message: '请选择付款方式',
            trigger: 'change'
          }
        ],
        franchiseFee: [
          {
            required: true,
            message: '请输入加盟费',
            trigger: 'blur'
          },
          {
            pattern: /^\d+(\.\d{1,2})?$/,
            message: '请输入正确的金额',
            trigger: 'blur'
          }
        ],
        finalPayDate: [
          {
            required: true,
            message: '请选择尾款日期',
            trigger: 'change'
          }
        ],
        orderStatus: [
          {
            required: true,
            message: '请选择订单状态',
            trigger: 'change'
          }
        ],
        amount: [
          {
            required: true,
            message: '请输入已收款金额',
            trigger: 'blur'
          },
          {
            pattern: /^\d+(\.\d{1,2})?$/,
            message: '请输入正确的金额',
            trigger: 'blur'
          }
        ],
        paymentDate: [
          {
            required: true,
            message: '请选择收款日期',
            trigger: 'change'
          }
        ],
        paymentMethod: [
          {
            required: true,
            message: '请选择支付方式',
            trigger: 'change'
          }
        ]
      },
      brandOptions: [],
      methodOptions: [{ fullName: '刷卡', id: '1' }, { fullName: '转账', id: '2' }, { fullName: '扫码', id: '3' }, { fullName: '现金', id: '4' }],
      typeOptions: [{ fullName: '加盟', id: '1' }, { fullName: '续约', id: '2' }, { fullName: '代理', id: '3' }],
      orderStatusOptions: [{ fullName: '待付款', id: '1' }, { fullName: '已付定金', id: '2' }, { fullName: '已付全款', id: '3' }, { fullName: '已完成', id: '4' }],
      managerIdOptions: [],
      statusOptions: [{ fullName: '全款', id: '1' }, { fullName: '定金', id: '2' }, { fullName: '保证金', id: '3' }, { fullName: '余款', id: '4' }]
    }
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {
    this.getUserList()
    this.$store.dispatch('base/getDictionaryData', { sort: 'Brand' }).then(res => {
      this.brandOptions = res
    })
  },
  methods: {
    getUserList() {
      this.$store.dispatch('base/getUserList').then(res => {
        this.managerIdOptions = res.filter(o => o.enabledMark == 1 && o.positionId.indexOf('445842201948194053') > -1)
      })
    },
    goBack() {
      this.$emit('refresh')
    },
    init(id, isDetail) {
      this.dataForm.id = id || 0
      this.visible = true
      this.isDetail = isDetail || false
      this.$nextTick(() => {
        this.$refs['elForm'].resetFields()
        if (this.dataForm.id) {
          request({
            url: '/api/crm/orders/' + this.dataForm.id,
            method: 'get'
          }).then(res => {
            this.dataForm = res.data
          })
        }
      })
    },
    dataFormSubmit() {
      console.log(this.dataForm, 'this.dataForm')
      this.$refs['elForm'].validate(valid => {
        if (valid) {
          if (!this.dataForm.id) {
            request({
              url: `/api/crm/orders`,
              method: 'post',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  ;(this.visible = false), this.$emit('refresh', true)
                }
              })
            })
          } else {
            request({
              url: '/api/crm/orders/' + this.dataForm.id,
              method: 'PUT',
              data: this.dataForm
            }).then(res => {
              this.$message({
                message: res.msg,
                type: 'success',
                duration: 1000,
                onClose: () => {
                  this.visible = false
                  this.$emit('refresh', true)
                }
              })
            })
          }
        }
      })
    }
  }
}
